﻿using System.Collections.Generic;
using umbraco.BusinessLogic;

namespace SoetemanSoftware.Umb.Datatypes.MacroPicker.Data
{
    /// <summary>
    /// Class that communicates with the Database
    /// </summary>
    internal class MacroData
    {
        #region Methods
        /// <summary>
        /// Store Allowed Macros in the database
        /// </summary>
        internal static void SavePreValues(string data, int dataDefinitionID)
        {
            Application.SqlHelper.ExecuteNonQuery("delete from cmsDataTypePreValues where datatypenodeid = @dtdefid", 
            Application.SqlHelper.CreateParameter("@dtdefid", dataDefinitionID));      
      
            Application.SqlHelper.ExecuteNonQuery("insert into cmsDataTypePreValues (datatypenodeid,[value],sortorder,alias) values (@dtdefid,@value,0,'')",
            Application.SqlHelper.CreateParameter("@dtdefid", dataDefinitionID), Application.SqlHelper.CreateParameter("@value", data));
        }

        /// <summary>
        /// Returns allowed Macros for the datatype from the database
        /// </summary>
        internal static List<string> GetPreValues(int dataDefinitionID)
        {
            List<string> result = new List<string>();
            string values = string.Format("{0}",Application.SqlHelper.ExecuteScalar<string>("select value from cmsDataTypePreValues where datatypenodeid = @datatypenodeid",
                                Application.SqlHelper.CreateParameter("@datatypenodeid", dataDefinitionID)));

            result.AddRange(values.Split(','));

            return result;
        }
        #endregion
    }
}
